package com.go.where.family.component.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.go.where.family.component.CustomerComponent;
import com.go.where.family.constants.Constant;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.jersey.api.EasemobIMUsers;
import com.go.where.family.pojo.AccountRelationInfo;
import com.go.where.family.pojo.CustomerClientInfo;
import com.go.where.family.pojo.CustomerInfo;
import com.go.where.family.pojo.CustomerScoreInfo;
import com.go.where.family.pojo.SmsAuthCodeInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.CustomerClientService;
import com.go.where.family.service.CustomerScoreService;
import com.go.where.family.service.CustomerService;
import com.go.where.family.service.SmsAuthCodeService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.utils.DateUtil;
import com.go.where.family.vo.CustomerVo;
import com.google.gson.Gson;

@Component("customerInfoComponent")
public class CustomerComponentImpl implements CustomerComponent {

	private static final Logger log = Logger
			.getLogger(CustomerComponentImpl.class);
	private Gson gson;
	@Resource(name = "smsService")
	private SmsAuthCodeService authCodeService;
	@Resource(name = "customerService")
	private CustomerService customerService;
	@Resource(name = "customerClientService")
	private CustomerClientService customerClientService;
	@Resource(name = "customerScoreService")
	private CustomerScoreService customerScoreService;

	public CustomerComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public CustomerVo getAuthCode(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			String mobile = vo.getMobile();
			List<SmsAuthCodeInfo> smsCodes = this.authCodeService
					.queryByMobile(mobile, head);
			if (smsCodes != null && smsCodes.size() > 10) {
				log
						.info("requestId:" + head.getRequestId()
								+ ",验证码获取次数超过3次限制。");
				throw new ServerDisponseException(
						TransmissionCode.AUTH_CODE_OVER_LIMIT.getCode(),
						TransmissionCode.AUTH_CODE_OVER_LIMIT.getMessage());
			}
			CustomerInfo userInfo = this.customerService.queryByMobile(vo
					.getMobile(), head);
			if ("1".equals(vo.getRegister())) {
				if (userInfo != null) {
					log.info("requestId:" + head.getRequestId() + ",此手机号码已注册。");
					throw new ServerDisponseException(
							TransmissionCode.MOBILE_IS_USED.getCode(),
							TransmissionCode.MOBILE_IS_USED.getMessage());
				}
			} else if ("0".equals(vo.getRegister())) {
				if (userInfo == null) {
					log.info("requestId:" + head.getRequestId()
							+ ",忘记密码时，此手机号码还没有注册。");
					throw new ServerDisponseException(
							TransmissionCode.MOBILE_IS_NOT_USED.getCode(),
							TransmissionCode.MOBILE_IS_NOT_USED.getMessage());
				}
			}
			CustomerVo user = new CustomerVo();
			user.setMobile(mobile);
			user.setAuthCode(CommonUtil.createRandomNumber());
			for (int i = 0; i < 20; i++) {
				SmsAuthCodeInfo info = this.authCodeService
						.queryByMobileAndCode(mobile, user.getAuthCode(), head);
				if (info != null) {
					user.setAuthCode(CommonUtil.createRandomNumber());
				} else {
					break;
				}
			}
			int re = this.saveAuthCode(user, head);
			if (re > 0) {
				return user;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取验证码时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	private int saveAuthCode(CustomerVo user, Head head) {
		int re = -1;
		try {
			SmsAuthCodeInfo info = new SmsAuthCodeInfo();
			info.setId(UUID.randomUUID().toString());
			info.setMobile(user.getMobile());
			info.setCreateTime(new Date());
			info.setAuthCode(user.getAuthCode());
			re = this.authCodeService.save(info, head);
		} catch (Exception e) {
			log.error("requestId:" + head.getRequestId() + ",保存短信验证码信息异常", e);
		}
		return re;
	}

	@Override
	public CustomerVo checkAuthCode(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			SmsAuthCodeInfo info = this.authCodeService.queryByMobileAndCode(vo
					.getMobile(), vo.getAuthCode(), head);
			if (info != null) {
				Date createAuthTime = info.getCreateTime();
				Date currentTime = new Date();
				int ten = 10 * 60;
				long between = (currentTime.getTime() - createAuthTime
						.getTime()) / 1000;
				if (between > ten) {
					log.info("requestId:" + head.getRequestId() + ",手机验证码已过时。");
					throw new ServerDisponseException(
							TransmissionCode.AUTH_CODE_TIME_OUT.getCode(),
							TransmissionCode.AUTH_CODE_TIME_OUT.getMessage());
				} else {
					CustomerVo u = new CustomerVo();
					u.setMobile(info.getMobile());
					u.setAuthCode(info.getAuthCode());
					return u;
				}
			} else {
				log.info("requestId:" + head.getRequestId() + ",验证码查询异常。");
				throw new ServerDisponseException(
						TransmissionCode.AUTH_CODE_ERROR.getCode(),
						TransmissionCode.AUTH_CODE_ERROR.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",校验验证码时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CustomerVo register(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerInfo userInfo = this.customerService.queryByMobile(vo
					.getMobile(), head);
			if (userInfo != null) {
				log.info("requestId:" + head.getRequestId() + ",此手机号码已注册。");
				throw new ServerDisponseException(
						TransmissionCode.MOBILE_IS_USED.getCode(),
						TransmissionCode.MOBILE_IS_USED.getMessage());
			}
			vo.setCustomerId(CommonUtil.createRandomNo(8));
			for (int i = 0; i < 20; i++) {
				CustomerInfo info = this.customerService.queryByPrimaryKey(vo
						.getCustomerId(), head);
				if (info != null) {
					vo.setCustomerId(CommonUtil.createRandomNo(8));
				} else {
					break;
				}
			}
			int re = this.saveUser(vo, head);
			if (re > 0) {
				updateDeviceId(head, vo);
				registerIMUser(head, vo);
				if (StringUtils.isNotBlank(vo.getInvitationCode())) {
					CustomerInfo u = this.customerService.queryByMyInviteCode(
							vo.getInvitationCode(), head);
					if (u != null) {
						String customerId = u.getCustomerId();
						int scRe = -1;
						CustomerScoreInfo score = new CustomerScoreInfo();
						score.setCustomerId(customerId);
						score.setId(UUID.randomUUID().toString());
						score.setScore(Constant.INVITATION_SCORE);
						score
								.setScoreSource(Constant.SCORE_SOURCE_INVITION_CODE);
						scRe = this.customerScoreService.save(score);
						if (scRe > 0) {
							Integer scoreDb = u.getCustomerScore();
							Integer newScore = CommonUtil.add(scoreDb,
									Constant.INVITATION_SCORE);
							CustomerInfo ci = new CustomerInfo();
							ci.setCustomerId(customerId);
							ci.setCustomerScore(newScore);
							customerService.update(ci, head);
						}
					}
				}
				return vo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",注册用户时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	private void registerIMUser(Head head, CustomerVo vo) {
		// 注册环信用户账号--- 注册IM用户[单个]
		ObjectNode result = EasemobIMUsers.registerIMUser(vo.getCustomerId(),
				vo.getCustomerId());
		if (result != null && 200 == result.get("statusCode").asInt()) {
			log.info("requestId:" + head.getRequestId() + ",注册用户时,注册环信用户账号成功。");
		}
	}

	private void updateDeviceId(Head head, CustomerVo vo) {
		if (StringUtils.isNotBlank(vo.getDeviceId())) {
			CustomerClientInfo cci = new CustomerClientInfo();
			cci.setDeviceId(vo.getDeviceId());
			cci.setCustomerId(vo.getCustomerId());
			cci.setInsertTime(new Date());
			if (StringUtils.isNotBlank(vo.getClientId())) {
				cci.setClientId(vo.getClientId());
			}
			CustomerClientInfo cc = customerClientService.queryByPrimaryKey(vo
					.getCustomerId());
			int cuRe = -1;
			if (cc != null) {
				cuRe = customerClientService.update(cci, head);
			} else {
				cuRe = this.customerClientService.save(cci, head);
			}
			if (cuRe <= 0) {
				log.info("requestId:" + head.getRequestId()
						+ ",用户登录时，更新设备ID异常。");
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		}
	}

	private int saveUser(CustomerVo user, Head head) {
		int re = -1;
		try {
			CustomerInfo info = new CustomerInfo();
			info.setCustomerId(user.getCustomerId());
			info.setMobile(user.getMobile());
			info.setCustomerPwd(user.getCustomerPwd());
			info.setRegisterTime(new Date());
			info.setStatus(Constant.USER_ON_LINE);
			info.setCustomerScore(Constant.REGISTER_SCORE);
			String cityAgentCode = "";
			String nextKey = "";
			if (StringUtils.isNoneBlank(user.getInvitationCode())) {
				String inviCode = user.getInvitationCode();
				info.setInvitationCode(inviCode);
				cityAgentCode = inviCode.substring(0, 2);
				nextKey = customerService.getNextInviteCode();
				info.setMyInvitationCode(cityAgentCode
						+ CommonUtil.createRandomNo(3) + nextKey);
			} else {
				info.setMyInvitationCode(CommonUtil.createRandomNo(8));
				for (int i = 0; i < 20; i++) {
					CustomerInfo u = this.customerService.queryByMyInviteCode(
							info.getMyInvitationCode(), head);
					if (u != null) {
						info.setMyInvitationCode(CommonUtil.createRandomNo(8));

					} else {
						break;
					}
				}
			}
			re = this.customerService.save(info, head);
		} catch (Exception e) {
			log.error("requestId:" + head.getRequestId() + ",保存用户信息异常", e);
		}
		return re;
	}

	@Override
	public CustomerVo forgetPassword(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerInfo user = new CustomerInfo();
			user.setMobile(vo.getMobile());
			user.setCustomerPwd(vo.getCustomerPwd());
			CustomerInfo userInfo = this.customerService.queryByMobile(vo
					.getMobile(), head);
			int re = this.customerService.updatePwd(user, head);
			if (re > 0 && userInfo != null) {
				vo.setCustomerId(userInfo.getCustomerId());
				return vo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",忘记密码时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CustomerVo login(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerInfo user = this.customerService.queryByMobile(vo
					.getMobile(), head);
			if (user == null || StringUtils.isBlank(user.getCustomerPwd())
					|| !user.getCustomerPwd().equals(vo.getCustomerPwd())) {
				log.info("requestId:" + head.getRequestId() + ",用户名或密码错误!");
				throw new ServerDisponseException(
						TransmissionCode.USER_OR_PASSWORD_ERROR.getCode(),
						TransmissionCode.USER_OR_PASSWORD_ERROR.getMessage());
			}
			if (Constant.USER_LOCKED.equals(user.getStatus())) {
				log.info("requestId:" + head.getRequestId() + ",用户被锁定!");
				throw new ServerDisponseException(
						TransmissionCode.USER_IS_LOCKED.getCode(),
						TransmissionCode.USER_IS_LOCKED.getMessage());
			}
			CustomerInfo userInfo = new CustomerInfo();
			userInfo.setCustomerId(user.getCustomerId());
			userInfo.setStatus(Constant.USER_ON_LINE);
			userInfo.setLastLoginTime(new Date());
			int re = this.customerService.update(userInfo, head);
			if (re > 0) {
				if (StringUtils.isNotBlank(vo.getDeviceId())) {
					vo.setCustomerId(user.getCustomerId());
					updateDeviceId(head, vo);
				}
				vo.setMobile(user.getMobile());
				vo.setCustomerId(userInfo.getCustomerId());
				return vo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}

		} else {
			log.info("requestId:" + head.getRequestId() + ",用户登录时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CustomerVo loginOf3Part(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			String account = vo.getAccount();
			String accountType = vo.getAccountType();
			AccountRelationInfo accountRelationInfo = new AccountRelationInfo();
			accountRelationInfo.setAccount(account);
			accountRelationInfo.setAccountType(accountType);
			List<AccountRelationInfo> ariList = customerService
					.queryAccRelaByParameter(accountRelationInfo);
			if (ariList != null && !ariList.isEmpty()) {
				String coustomerId = ariList.get(0).getCustomerId();
				CustomerInfo userInfo = this.customerService.queryByPrimaryKey(
						coustomerId, head);
				if (userInfo != null) {
					log.info("requestId:" + head.getRequestId()
							+ ",第三方信息已注册，直接登录");
					if (Constant.USER_LOCKED.equals(userInfo.getStatus())) {
						log
								.info("requestId:" + head.getRequestId()
										+ ",用户被锁定!");
						throw new ServerDisponseException(
								TransmissionCode.USER_IS_LOCKED.getCode(),
								TransmissionCode.USER_IS_LOCKED.getMessage());
					}
					CustomerInfo user = new CustomerInfo();
					user.setCustomerId(userInfo.getCustomerId());
					user.setStatus(Constant.USER_ON_LINE);
					user.setLastLoginTime(new Date());
					int re = this.customerService.update(user, head);
					if (re > 0) {
						CustomerClientInfo cci = this.customerClientService
								.queryByPrimaryKey(user.getCustomerId());
						if (StringUtils.isNotBlank(vo.getDeviceId())) {
							if (cci != null) {
								// 更新deviceId
								vo.setCustomerId(user.getCustomerId());
								updateDeviceId(head, vo);
							}
						}
						vo.setMobile(userInfo.getMobile());
						vo.setCustomerId(userInfo.getCustomerId());
						vo.setStatus("1");
						return vo;
					} else {
						throw new ServerDisponseException(
								TransmissionCode.OPERATOR_EXCEPTION.getCode(),
								TransmissionCode.OPERATOR_EXCEPTION
										.getMessage());
					}
				}
			}
			vo.setCustomerId(CommonUtil.createRandomNo(8));
			for (int i = 0; i < 20; i++) {
				CustomerInfo info = this.customerService.queryByPrimaryKey(vo
						.getCustomerId(), head);
				if (info != null) {
					vo.setCustomerId(CommonUtil.createRandomNo(8));
				} else {
					break;
				}
			}
			int re = this.saveUser(vo, head);
			if (re > 0) {
				AccountRelationInfo acco = new AccountRelationInfo();
				acco.setCustomerId(vo.getCustomerId());
				acco.setAccount(vo.getAccount());
				acco.setAccountType(vo.getAccountType());
				customerService.saveBindAccount(acco);
				updateDeviceId(head, vo);
				registerIMUser(head, vo);
				vo.setStatus("0");
				return vo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",第三方账号登录时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CustomerVo fastLogin(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerInfo userInfo = this.customerService.queryByMobile(vo
					.getMobile(), head);
			if (userInfo != null) {
				log.info("requestId:" + head.getRequestId() + ",此手机号码已注册，直接登录");
				if (Constant.USER_LOCKED.equals(userInfo.getStatus())) {
					log.info("requestId:" + head.getRequestId() + ",用户被锁定!");
					throw new ServerDisponseException(
							TransmissionCode.USER_IS_LOCKED.getCode(),
							TransmissionCode.USER_IS_LOCKED.getMessage());
				}
				CustomerInfo user = new CustomerInfo();
				user.setCustomerId(userInfo.getCustomerId());
				user.setStatus(Constant.USER_ON_LINE);
				user.setLastLoginTime(new Date());
				int re = this.customerService.update(user, head);
				if (re > 0) {
					CustomerClientInfo cci = this.customerClientService
							.queryByPrimaryKey(user.getCustomerId());
					if (StringUtils.isNotBlank(vo.getDeviceId())) {
						if (cci != null) {
							// 更新deviceid
							vo.setCustomerId(user.getCustomerId());
							updateDeviceId(head, vo);
						}
					}
					vo.setMobile(userInfo.getMobile());
					vo.setCustomerId(userInfo.getCustomerId());
					vo.setStatus("1");
					log.info("快速登录返回结果：" + gson.toJson(vo));
					return vo;
				} else {
					throw new ServerDisponseException(
							TransmissionCode.OPERATOR_EXCEPTION.getCode(),
							TransmissionCode.OPERATOR_EXCEPTION.getMessage());
				}
			}
			vo.setCustomerId(CommonUtil.createRandomNo(8));
			for (int i = 0; i < 20; i++) {
				CustomerInfo info = this.customerService.queryByPrimaryKey(vo
						.getCustomerId(), head);
				if (info != null) {
					vo.setCustomerId(CommonUtil.createRandomNo(8));
				} else {
					break;
				}
			}
			int re = this.saveUser(vo, head);
			if (re > 0) {
				updateDeviceId(head, vo);
				registerIMUser(head, vo);
				// 绑定账号
				AccountRelationInfo account = new AccountRelationInfo();
				account.setMobile(vo.getMobile());
				account.setCustomerId(vo.getCustomerId());
				customerService.saveBindAccount(account);
				vo.setStatus("0");
				log.info("快速登录返回结果：" + gson.toJson(vo));
				return vo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",用户快速登录时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean updateAccount(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerInfo info = new CustomerInfo();
			info.setHeaderPath(vo.getHeaderPath());
			info.setCustomerId(vo.getCustomerId());
			info.setSex(vo.getSex());
			info.setCustomerPwd(vo.getCustomerPwd());
			// 判断昵称是否重复
			if (StringUtils.isNoneBlank(vo.getCustomerNick())) {
				CustomerInfo cInfo = customerService.queryByCustomerNick(vo
						.getCustomerNick(), head);
				if (cInfo != null) {
					throw new ServerDisponseException(
							TransmissionCode.NICK_EXIST.getCode(),
							TransmissionCode.NICK_EXIST.getMessage());
				}
			}
			info.setCustomerNick(vo.getCustomerNick());

			if (!StringUtils.isEmpty(vo.getBirthday())) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				try {
					info.setBirthday(sdf.parse(vo.getBirthday()));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			if (StringUtils.isNotBlank(vo.getInvitationCode())) {
				CustomerInfo u = this.customerService.queryByMyInviteCode(vo
						.getInvitationCode(), head);
				if (u != null) {
					String customerId = u.getCustomerId();
					int scRe = -1;
					CustomerScoreInfo score = new CustomerScoreInfo();
					score.setCustomerId(customerId);
					score.setId(UUID.randomUUID().toString());
					score.setScore(Constant.INVITATION_SCORE);
					score.setScoreSource(Constant.SCORE_SOURCE_INVITION_CODE);
					scRe = this.customerScoreService.save(score);
					if (scRe > 0) {
						Integer scoreDb = u.getCustomerScore();
						Integer newScore = CommonUtil.add(scoreDb,
								Constant.INVITATION_SCORE);
						CustomerInfo ci = new CustomerInfo();
						ci.setCustomerId(customerId);
						ci.setCustomerScore(newScore);
						customerService.update(ci, head);
					}
				}
			}
			info.setInvitationCode(vo.getInvitationCode());
			int re = this.customerService.update(info, head);
			if (re > 0) {
				return true;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",修改用户信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CustomerVo getAccount(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			String userId = vo.getCustomerId();
			CustomerInfo userinfo = this.customerService.queryByPrimaryKey(
					userId, head);
			if (userinfo != null) {
				vo.setCustomerId(userinfo.getCustomerId());
				vo.setInvitationCode(userinfo.getMyInvitationCode());
				vo.setCustomerNick(userinfo.getCustomerNick());
				vo.setSex(userinfo.getSex());
				vo.setBirthday(DateUtil.format(userinfo.getBirthday()));
				if (StringUtils.isNotBlank(userinfo.getHeaderPath())) {
					vo.setHeaderPath(CommonUtil
							.getProperties("photo.url.prefix.path.api")
							+ userinfo.getHeaderPath());
				}
				Integer score = userinfo.getCustomerScore();
				Integer customerLevel = CommonUtil.getCustomerLevel(score);
				vo.setCustomerLevel(customerLevel);
				vo.setCustomerScore(userinfo.getCustomerScore());
				return vo;
			} else {
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取账户信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}
	
	@Override
	public CustomerVo updatePwd(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerInfo userinfo = this.customerService.queryByPrimaryKey(vo
					.getCustomerId(), head);
			if (userinfo == null
					|| !vo.getOldPassword().equals(userinfo.getCustomerPwd())) {
				throw new ServerDisponseException(
						TransmissionCode.OLD_PASSWORD_ERROR.getCode(),
						TransmissionCode.OLD_PASSWORD_ERROR.getMessage());
			}
			CustomerInfo user = new CustomerInfo();
			user.setCustomerId(vo.getCustomerId());
			user.setCustomerPwd(vo.getCustomerPwd());
			int re = this.customerService.update(user, head);
			if (re > 0) {
				return vo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",修改密码时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean checkUser(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerClientInfo cci = this.customerClientService
					.queryByPrimaryKey(vo.getCustomerId());
			if (cci != null && vo.getDeviceId().equals(cci.getDeviceId())) {
				return true;
			} else {
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",校验验用户时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CustomerVo uploadCilentId(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerClientInfo cci = this.customerClientService
					.queryByPrimaryKey(vo.getCustomerId());
			CustomerClientInfo ccInfo = new CustomerClientInfo();
			ccInfo.setCustomerId(vo.getCustomerId());
			ccInfo.setClientId(vo.getClientId());
			int re = -1;
			if (cci != null) {
				ccInfo.setUpdateTime(new Date());
				re = this.customerClientService.update(ccInfo, head);
			} else {
				ccInfo.setInsertTime(new Date());
				re = this.customerClientService.save(ccInfo, head);
			}
			if (re > 0) {
				return vo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",上传设备唯一标示时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public CustomerVo getPersonalInfo(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerInfo usr = this.customerService.queryByPrimaryKey(vo
					.getCustomerId(), head);
			if (usr != null) {
				CustomerVo uvo = new CustomerVo();
				uvo.setCustomerId(usr.getCustomerId());
				if (StringUtils.isNotBlank(usr.getCustomerNick())) {
					uvo.setCustomerNick(usr.getCustomerNick());
				} else {
					uvo.setCustomerNick(usr.getCustomerId());
				}
				if (StringUtils.isNoneBlank(usr.getHeaderPath())) {
					uvo.setHeaderPath(CommonUtil
							.getProperties("photo.url.prefix.path.api")
							+ usr.getHeaderPath());
				}
				uvo.setBirthday(DateUtil.format(usr.getBirthday()));
				uvo.setSex(usr.getSex());
				return uvo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取个人用户信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<CustomerVo> matchMobiles(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			String[] mobileList = vo.getMobiles().split(";");
			List<CustomerInfo> users = null;
			if (mobileList != null && mobileList.length > 0) {
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("mobileList", mobileList);
				users = this.customerService.getUserListByMobiles(paraMap);
			}
			List<CustomerVo> userList = new ArrayList<CustomerVo>();
			if (users != null && !users.isEmpty()) {
				for (CustomerInfo user : users) {
					CustomerVo uv = new CustomerVo();
					uv.setCustomerId(user.getCustomerId());
					if (StringUtils.isNotBlank(user.getCustomerNick())) {
						uv.setCustomerNick(user.getCustomerNick());
					} else {
						uv.setCustomerNick(user.getCustomerId());
					}
					if (StringUtils.isNoneBlank(user.getHeaderPath())) {
						uv.setHeaderPath(CommonUtil
								.getProperties("photo.url.prefix.path.api")
								+ user.getHeaderPath());
					}
					uv.setMobile(user.getMobile());
					userList.add(uv);
				}
			}
			return userList;
		} else {
			log.info("requestId:" + head.getRequestId() + ",匹配用户手机号码时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<CustomerVo> search(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			// String userId = vo.getCustomerId();
			// 过滤掉已关注的用户
			// CustomerAttentionInfo att = new CustomerAttentionInfo();
			// att.setAttentionId(userId);
			// List<String> userIds = this.customerAttentionService
			// .queryByAidOrBeid(att);
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			// if (userIds != null && !userIds.isEmpty()) {
			// paraMap.put("userIds", userIds);
			// }
			paraMap.put("search", vo.getSearch());
			List<CustomerVo> list = new ArrayList<CustomerVo>();
			List<CustomerInfo> userList = this.customerService
					.queryPage(paraMap);
			if (userList != null && !userList.isEmpty()) {
				for (CustomerInfo usr : userList) {
					CustomerVo uvo = new CustomerVo();
					uvo.setCustomerId(usr.getCustomerId());
					if (StringUtils.isNotBlank(usr.getCustomerNick())) {
						uvo.setCustomerNick(usr.getCustomerNick());
					} else {
						uvo.setCustomerNick(usr.getCustomerId());
					}
					if (StringUtils.isNoneBlank(usr.getHeaderPath())) {
						uvo.setHeaderPath(CommonUtil
								.getProperties("photo.url.prefix.path.api")
								+ usr.getHeaderPath());
					}
					uvo.setBirthday(DateUtil.format(usr.getBirthday()));
					uvo.setSex(usr.getSex());
					list.add(uvo);
				}
			}
			return list;
		} else {
			log.info("requestId:" + head.getRequestId() + ",搜索用户列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean bindOrCancel(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			String bindOrNot = vo.getBindOrNot();
			int result = 0;
			AccountRelationInfo account = new AccountRelationInfo();
			account.setAccount(vo.getAccount());
			account.setAccountType(vo.getAccountType());
			account.setMobile(vo.getMobile());
			if ("1".equals(bindOrNot)) {
				if (StringUtils.isNotBlank(vo.getMobile())) {
					CustomerInfo ci = customerService.queryByMobile(vo
							.getMobile(), head);
					if (ci != null) {
						log.info("requestId:" + head.getRequestId()
								+ ",绑定账号时，此手机号码已绑定。");
						throw new ServerDisponseException(
								TransmissionCode.ACCOUNT_BINDED.getCode(),
								TransmissionCode.ACCOUNT_BINDED.getMessage());
					} else {
						CustomerInfo cui = new CustomerInfo();
						cui.setMobile(vo.getMobile());
						cui.setCustomerId(vo.getCustomerId());
						customerService.update(cui, head);
					}
				}
				List<AccountRelationInfo> ariList = customerService
						.queryAccRelaByParameter(account);
				if (ariList != null && ariList.size() >= 1) {
					log.info("requestId:" + head.getRequestId()
							+ ",绑定账号时，此账号已绑定。");
					throw new ServerDisponseException(
							TransmissionCode.ACCOUNT_BINDED.getCode(),
							TransmissionCode.ACCOUNT_BINDED.getMessage());
				} else {
					account.setCustomerId(vo.getCustomerId());
					result = customerService.saveBindAccount(account);
				}
			} else {
				account = new AccountRelationInfo();
				account.setCustomerId(vo.getCustomerId());
				List<AccountRelationInfo> ariList = customerService
						.queryAccRelaByParameter(account);
				if (ariList != null && ariList.size() >= 2) {
					account.setAccount(vo.getAccount());
					account.setAccountType(vo.getAccountType());
					account.setMobile(vo.getMobile());
					if (StringUtils.isNotBlank(vo.getMobile())) {
						CustomerInfo ci = new CustomerInfo();
						ci.setMobile("");
						ci.setCustomerId(vo.getCustomerId());
						customerService.update(ci, head);
					}
					result = customerService.deleteBindAccount(account);
				} else {
					log.info("requestId:" + head.getRequestId()
							+ ",解绑账号时，至少要保留有一个绑定账号。");
					throw new ServerDisponseException(
							TransmissionCode.ACCOUNT_BIND_MUST_ONE.getCode(),
							TransmissionCode.ACCOUNT_BIND_MUST_ONE.getMessage());
				}
			}
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",绑定账号时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<AccountRelationInfo> queryBindList(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			AccountRelationInfo account = new AccountRelationInfo();
			account.setCustomerId(vo.getCustomerId());
			List<AccountRelationInfo> ariList = customerService
					.queryAccRelaByParameter(account);
			if (ariList != null && !ariList.isEmpty()) {
				return ariList;
			} else {
				return new ArrayList<AccountRelationInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",查询绑定账号列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
